<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="80px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'" size="mini">未付款</el-tag>
            <el-tag v-else size="mini">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="80px"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">{{ scope.row.create_time | dataformat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope>
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editDialogVisible = true"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="定位" placement="top" :enterable="false">
              <el-button
                type="success"
                icon="el-icon-location-outline"
                size="mini"
                @click="getScheduleList"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- 分页导航区域
      @size-change(pagesize改变时触发)
      @current-change(页码发生改变时触发)
      :current-page(设置当前页码)
      :page-size(设置每页的数据条数)
      :total(设置总页数)-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 8, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="60%" @close="closeEditForm">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省市区/县" prop="name">
          <el-cascader
            v-model="editForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="scheduleDialogVisible" width="60%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in ScheduleList"
          :key="index"
          :timestamp="item.time"
        >{{item.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/js/citydata.js'
export default {
  data () {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      // 订单列表
      orderList: [],
      // 总数据条数
      total: 0,
      // 编辑对话框的显示与隐藏
      editDialogVisible: false,
      // 修改地址表单
      editForm: {
        address1: [],
        address2: ''
      },
      // 修改地址表单验证
      editFormRules: {
        address1: [
          { required: true, message: '请输入省市', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 省级联动
      cityData,
      // 物流进度对话框的显示与隐藏
      scheduleDialogVisible: false,
      // 物流信息列表
      ScheduleList: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取用户订单数据
    async getOrderList() {
      const { data: res } = await this.axios.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.msg.error('获取用户订单失败')
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 修改了每页数据展示条数
    handleSizeChange(num) {
      this.queryInfo.pagesize = num
      this.getOrderList()
    },
    // 修改了当前页码
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getOrderList()
    },
    // 点击关闭修改地址对话框重置表单
    closeEditForm() {
      this.$refs.editFormRef.resetFields()
      this.editForm.address1 = []
    },
    // 获取物流进度
    async getScheduleList() {
      const { data: res } = await this.axios.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.msg.error('获取物流信息失败')
      this.ScheduleList = res.data
      this.scheduleDialogVisible = true
    }
  }
}
</script>

<style scoped lang="less">
  .el-cascader {
    width: 100%;
  }
</style>
